package com.soundShape.mediators
{
	import com.soundShape.constants.SoundShapeConstants;
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.photos.PhotosMediator;
	import com.soundShape.model.ApplicationModel;
	import com.soundShape.views.MainView;
	
	import flash.data.SQLConnection;
	import flash.desktop.NativeApplication;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	
	import nz.co.codec.flexorm.EntityManager;
	
	import org.swizframework.storage.ISharedObjectBean;
	
	import spark.components.DataGroup;
	import spark.components.TabBar;
	import spark.effects.Fade;
	
	

	public class MainViewMediator extends AbstractViewMediator
	{
		
		public var loginName:String = "Foo Foo";
		
		[Inject]
		public var photosMediator:PhotosMediator;
		
		public function MainViewMediator()
		{
			super(MainView);
		}
		
		/**
		 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
		 * we need to create a local getter to access the underlying, expected view
		 * class type.
		 */
		public function get view():MainView
		{
			return this._view as MainView;
		}
		
		/**
		 * The <code>init()</code> method is fired off automatically by the 
		 * AbstractViewMediator when the creation complete event fires for the
		 * corresponding ViewMediator's view. This allows us to listen for events
		 * and set data bindings on the view with the confidence that our view
		 * and all of it's child views have been created and live on the stage.
		 */
		override protected function init():void
		{
			super.init();
			
			this.setViews();
			
		}
		
		override protected function setViewDataBindings():void
		{
			super.setViewDataBindings();
			
			BindingUtils.bindProperty(this.view.lblAppVersion,"text",this.applicationModel,"applicationVersion");
		}	
		
		override protected function setViewListeners():void
		{
			super.setViewListeners();
			
			this.view.lblLogout.addEventListener(MouseEvent.CLICK, handleLogout);
			this.view.lblLogout.addEventListener(MouseEvent.MOUSE_OVER, handleLogout);
			this.view.lblLogout.addEventListener(MouseEvent.MOUSE_OUT, handleLogout);
			
		}
		
		private function setViews():void
		{
			doVersion();
			
			setEntityManager();
		}
		
		
		
		/**
		 * *******************************************************************************
		 * Handlers
		 * *******************************************************************************
		 */
		
		//private var sharedObjectUtil:SharedObjectUtil = new SharedObjectUtil();
		
		protected function handleBtnGetLSOClick(event:MouseEvent):void
		{
			//var value:Object = sharedObjectUtil.getValue(this.view.txtInGetName.text);
			//this.view.lblLsoValue.text = value.toString();
		}
		
		protected function handleBtnSetLSOClick(event:MouseEvent):void
		{
			//sharedObjectUtil.saveValue(this.view.txtInSetName.text, this.view.txtInSetValue.text);
		}
		
		protected function handleLogout(event:MouseEvent):void
		{
			switch(event.type)
			{
				case MouseEvent.CLICK:
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_LOGIN_VIEW));
					break;
				
				case MouseEvent.MOUSE_OVER:
					this.view.lblLogout.setStyle("fontWeight", "bold");
					break;
				
				case MouseEvent.MOUSE_OUT:
					this.view.lblLogout.setStyle("fontWeight", "normal");
					break;
			}
		}
		
		
		/**
		 * *******************************************************************************
		 * Swiz EventHanders
		 * *******************************************************************************
		 */
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_ADMIN_VIEW")]
		public function doSwitchAdministrationView(soundShapeEvent:SoundShapeEvent):void
		{
			if(this.applicationModel.employeeVO.role == SoundShapeConstants.ROLE_ADMINISTRATOR)
				this.view.currentState = "administration";
			else
				Alert.show("This feature available to Administrators only","Alert");
		
			
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_FOLLOW_UP_VIEW")]
		public function doSwitchFollowUpView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "followUp";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_FREE_CONSULTATION_VIEW")]
		public function doSwitchFreeConsultationView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "freeConsultation";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_LOGIN_VIEW")]
		public function doSwitchLoginView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.lblLoginName.text = "";
			
			if(soundShapeEvent.data != null)
			{
				if(soundShapeEvent.data.duplicateData)
				{
					this.view.login.lblLoginInfo.text = "Attempted duplication of records";
				}
				else
				{
					this.view.login.lblLoginInfo.text = "";
				}
			}
			
			this.view.currentState = "login";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_MENU_VIEW")]
		public function doSwitchMenuView(soundShapeEvent:SoundShapeEvent):void
		{
			this.loginName = "Welcome: " + this.applicationModel.employeeVO.firstName + " " + this.applicationModel.employeeVO.lastName;
			this.view.lblLogout.text = "[ logout ]";
			BindingUtils.bindProperty(this.view.lblLoginName,"text",this,"loginName");
			this.view.currentState = "menu";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_NEW_CUSTOMER_VIEW")]
		public function doSwitchNewCustomerView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "newCustomer";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_NEW_EMPLOYEE_VIEW")]
		public function doSwitchNewEmployeeView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "newEmployee";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_PHOTOS_VIEW")]
		public function doSwitchPhotosView(soundShapeEvent:SoundShapeEvent):void
		{
			// clear out photos
			photosMediator.clearPhotoTile();
			this.view.currentState = "photos";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_TRANSACTIONS_VIEW")]
		public function doSwitchTransactionsView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "transactions";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_TREATMENT_VIEW")]
		public function doSwitchTreatmentView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "treatment";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_REPORTING_VIEW")]
		public function doSwitchReportingView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "reporting";
		}
		
		[EventHandler(event="SoundShapeEvent.SWITCH_TO_COMPARISON_VIEW")]
		public function doSwitchComparisonView(soundShapeEvent:SoundShapeEvent):void
		{
			this.view.currentState = "comparison";
		}
		
		/** ************************************************************************
		 *                utils
		 *  ************************************************************************/
		
		// get the app version
		private function doVersion():void
		{
			var appDescriptor:XML = NativeApplication.nativeApplication.applicationDescriptor;
			var ns:Namespace = appDescriptor.namespace();
			this.applicationModel.applicationVersion = appDescriptor.ns::version;
		}
		
		//set up the ORM entity manager
		private function setEntityManager():void
		{
			var dbFile:File = File.applicationStorageDirectory.resolvePath( "SoundShapeProd.db" );
			var sqlConnection:SQLConnection = new SQLConnection();
			sqlConnection.open( dbFile );
			this.applicationModel.entityManager.sqlConnection = sqlConnection;
			
			
		}
		
		/**
		 * Remove any listeners we've created.
		 */
		override protected function cleanup( event:Event ):void
		{
			super.cleanup(event);
		}


	}
}